package com.cvnert.testplatform.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cvnert.testplatform.domain.User;
import com.cvnert.testplatform.domain.vo.UserVo;
import com.cvnert.testplatform.service.UserService;
import com.cvnert.testplatform.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* @author pc
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-03-24 14:48:24
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Resource
    private UserMapper userMapper;

    @Override
    public Object login(User user) {
        Map<String,Object> res = new HashMap<>();
        boolean flag = false;
        String msg = "用户名或密码不允许为空！";
        if(user!=null && user.getUsername()!=null && user.getPassword()!=null){
            User user1 = userMapper.selectOne(new QueryWrapper<User>().eq("username",user.getUsername()));
            if(user1!=null) {
                if(user.getPassword().equals(user1.getPassword())){
                    flag = true;
                    StpUtil.login(user1.getId());
                    res.put("token", StpUtil.getTokenValue());
                    res.put("uid",user1.getId());
                    res.put("role", user1.getRole());
                    msg="验证成功";
                }else{
                    msg = "密码不正确";
                }
            } else {
                msg = "用户名不存在";
            }
        }

        res.put("state",flag);
        res.put("msg",msg);
        return res;
    }

    @Override
    public Object register(User user) {
        Map<String,Object> res = new HashMap<>();
        String username = user.getUsername();
        String password = user.getPassword();
        if (equals(username) || equals(password)) {
            res.put("state",false);
            res.put("msg","用户名或密码不允许为空！");
            return res;
        }
        if(user!=null && user.getUsername()!=null && user.getPassword()!=null){
            User user1 = userMapper.selectOne(new QueryWrapper<User>().eq("username",user.getUsername()));
            if(user1!=null) {
                res.put("state",false);
                res.put("msg","用户名已存在");
            } else {
                user.setCreatetime(LocalDateTime.now());
                if (SaFoxUtil.isEmpty(user.getRole())) {
                    user.setRole("2");
                }else {
                    user.setRole(user.getRole());
                }
                user.setNick(user.getNick());
                user.setAvatar("https://img2.baidu.com/it/u=937770946,3517721897&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1711386000&t=81d01ba1a05eec202e7d028d6968a064");
                userMapper.insert(user);
                res.put("state",true);
                res.put("msg","注册成功");
            }
        } else {
            res.put("state",false);
            res.put("msg","用户名或密码不允许为空！");
        }
        return res;
    }

    @Override
    public Object getUserList(UserVo userVo) {
        Map<String,Object> res = new HashMap<>();
        // 判断pageNum和pageSize是否为空
        if (SaFoxUtil.isEmpty(userVo.getPageNum()) || SaFoxUtil.isEmpty(userVo.getPageSize())) {
            res.put("state",false);
            res.put("msg","pageNum和pageSize不允许为空！");
            return res;
        }
        Page<User> page = new Page<>(userVo.getPageNum(),userVo.getPageSize());
        //查询条件
        QueryWrapper<User> wrapper =new QueryWrapper<>();
        if(!SaFoxUtil.isEmpty(userVo.getUsername())){
            wrapper.like("username",userVo.getUsername());
        }
        if(!SaFoxUtil.isEmpty(userVo.getNick())){
            wrapper.like("nick",userVo.getNick());
        }
        if (!SaFoxUtil.isEmpty(userVo.getRole())){
            wrapper.eq("role",userVo.getRole());
        }
        res.put("state",true);
        res.put("msg","查询成功");
        res.put("data",userMapper.selectPage(page,wrapper));
        return res;
    }

    @Override
    public Object updateUser(User user) {
        HashMap<String, Object> res = new HashMap<>();
        if (SaFoxUtil.isEmpty(user.getId())) {
            res.put("state",false);
            res.put("msg","用户id不允许为空！");
            return res;
        }
        // 判断用户是否存在
        User user1 = userMapper.selectById(user.getId());
        if (SaFoxUtil.isEmpty(user1)) {
            res.put("state",false);
            res.put("msg","用户不存在");
            return res;
        }
        int i = userMapper.updateById(user);
        if (i > 0) {
            res.put("state",true);
            res.put("msg","修改成功");
        } else {
            res.put("state",false);
            res.put("msg","修改失败");
        }
        return res;
    }

    @Override
    public Object deleteUser(Integer uid) {
        HashMap<String, Object> res = new HashMap<>();
        if (SaFoxUtil.isEmpty(uid)) {
            res.put("state",false);
            res.put("msg","用户id不允许为空！");
            return res;
        }
        int i = userMapper.deleteById(uid);
        if (i > 0) {
            res.put("state",true);
            res.put("msg","删除成功");
        } else {
            res.put("state",false);
            res.put("msg","删除失败");
        }
        return res;
    }

    @Override
    public Object getUserById(Integer uid) {
        HashMap<String, Object> res = new HashMap<>();
        if (SaFoxUtil.isEmpty(uid)) {
            res.put("state",false);
            res.put("msg","用户id不允许为空！");
            return res;
        }
        User user = userMapper.selectById(uid);
        user.setPassword(null);
        if (user != null) {
            res.put("state",true);
            res.put("msg","查询成功");
            res.put("data",user);
        } else {
            res.put("state",false);
            res.put("msg","查询失败");
        }
        return res;
    }

}




